package CloudStorage;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class User
{
    int id;
    String username;
    String password;
    String email;
    String name;
    String surname;
    int credits;
    int availableSpace;
    String registrationDate;
    String lastLogin;

    public User ( String username, String password, String email, String name,  String surname )
    {
        this.username = username;
        this.password = password;
        this.email = email;
        this.name = name;
        this.surname = surname;
        this.credits = 0;
        this.availableSpace = 0;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date date = new java.util.Date();
        this.registrationDate = dateFormat.format(date);
    }

    public User ( int id, String username, String password, String email, String name,  String surname,
                int credits, int availableSpace, String registrationDate, String lastLogin )
    {
        this.id = id;
        this.username = username;
        this.password = password;
        this.email = email;
        this.name = name;
        this.surname = surname;
        this.credits = credits;
        this.availableSpace = availableSpace;
        this.registrationDate = registrationDate;
        this.lastLogin = lastLogin;
    }

    public int getId() { return id; }
    public String getUsername() { return username; }
    public String getPassword() { return password; }
    public String getEmail() { return email; }
    public String getSurname() { return surname; }
    public String getName() { return name; }
    public int getCredits() { return credits; }
    public int getAvailableSpace() { return availableSpace; }
    public String getRegistrationDate() { return registrationDate; }
    public String getLastLogin() { return lastLogin; }
    public long getUsedSpace(java.io.File dir) 
    {
        long size = 0;
        if (dir.isFile()) 
        {
            size = dir.length();
        }
        else 
        {
            java.io.File[] subFiles = dir.listFiles();

        for (java.io.File file : subFiles) 
        {
            if (file.isFile()) 
            {
                size += file.length();
            } 
//            else
  //          {
      //          size += this.getUsedSpace(file);
    //        }
        }   
        }

        return size;
    }
    public long getBonusSpace()
    {
        int yearRegistered, year;
        int monthRegistered, month;
        int dayRegistered, day;

        String dateString = Utility.getDate();

        yearRegistered = Integer.parseInt(this.getRegistrationDate().substring(0, 4));
        year = Integer.parseInt(dateString.substring(0, 4));

        monthRegistered = Integer.parseInt(this.getRegistrationDate().substring(5, 7));
        month = Integer.parseInt(dateString.toString().substring(5, 7));

        dayRegistered = Integer.parseInt(this.getRegistrationDate().substring(8, 10));
        day = Integer.parseInt(dateString.substring(8, 10));
                                                                                                    //bytes --> Mb
        return (( year - yearRegistered ) * 365 + ( month - monthRegistered ) * 30 + day - dayRegistered) * 1024 * 1024;
    }
    public long getPaidSpace()
    {
        java.io.File dir = new java.io.File(username);
        return availableSpace + this.getUsedSpace(dir) - this.getBonusSpace();
    }

    public static boolean deleteDirectories(java.io.File dir)
    {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i=0; i<children.length; i++) {
                boolean success = deleteDirectories(new java.io.File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }

        // The directory is now empty so delete it
        return dir.delete();
    }

}
